<?php
/**
 * Created by PhpStorm.
 * User: Duanwangye
 * Date: 18/09/21
 * Company:财联集惠
 */

namespace app\backstage\controller;

use app\model\UserAddress as Model;
class UserAddress extends Base
{
    //获取列表
    public function index() {

        $page = $this->request->post('page', 1);
        $size = $this->request->post('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $where = [];
        if( isset($this->request->post()['userID']) ) {
            $where['userID'] = $this->request->post()['userID'];
        }
        $model = new Model();
        $list = $model
            ->field('addressID,userID,consignee,address,mobile,isDefault,latitude,longitude,addTime,updataTime')
            ->limit(($page-1)*$size, $size)
            ->where($where)
            ->select();
        $count = $model ->where($where)->count();   //总条数

        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$list
        ]);
    }



    //新增banner图片
    public function saveAddress() {
        $postData = $this->request->post();
        $params = [
            'userID' => $postData['userID'],
            'consignee' => $postData['consignee'],
            'address' => $postData['address'],
            'mobile' => $postData['mobile'],
            'isDefault' => $postData['isDefault'],
            'latitude' => $postData['latitude'],
            'longitude' => $postData['longitude'],
        ];
        if(isset($postData['addressID'])){
            $where['addressID'] = $postData['addressID'];
            $crate_result = Model::where($where)->update($params);
        }else{
            $crate_result = Model::create($params);
        }
        if ($crate_result){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-1,'修改失败');
        }
    }

    /*
     * @name 删除banner图
     * @param bannerIDS
     */
    public function delAddress(){
        $postData = $this->request->post();
        $res = Model::destroy($postData['addressID']);
        if($res){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-2,'操作失败');
        }
    }

}